package tech.lq0.dreamaticvoyage.tools;

import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import tech.lq0.dreamaticvoyage.block.entity.FukamizuBoosterBlockEntity;
import tech.lq0.dreamaticvoyage.capability.ModCapabilities;
import tech.lq0.dreamaticvoyage.init.BlockRegistry;

import java.util.List;

public class FukamizuTechHelper {

    public static void popItems(List<ItemStack> list, BlockEntity entity) {
        if (entity.getLevel() == null) return;

        for (ItemStack item : list) {
            ItemEntity itemEntity = new ItemEntity(entity.getLevel(),
                    entity.getBlockPos().getX() + 0.5,
                    entity.getBlockPos().getY() - 0.2,
                    entity.getBlockPos().getZ() + 0.5,
                    item);
            entity.getLevel().addFreshEntity(itemEntity);
        }
    }

    public static void popItems(List<ItemStack> list, BlockEntity entity, DirectionProperty direction) {
        popItems(list, entity, direction, 0);
    }

    public static void popItems(List<ItemStack> list, BlockEntity entity, DirectionProperty direction, int energyCost) {
        if (entity.getLevel() == null) return;

        if (energyCost == 0) {
            for (ItemStack item : list) {
                ItemEntity itemEntity = new ItemEntity(entity.getLevel(),
                        entity.getBlockPos().getX() + entity.getBlockState().getValue(direction).getOpposite().getStepX() + 0.5,
                        entity.getBlockPos().getY() + 0.2,
                        entity.getBlockPos().getZ() + entity.getBlockState().getValue(direction).getOpposite().getStepZ() + 0.5,
                        item);
                entity.getLevel().addFreshEntity(itemEntity);
            }
        } else {
            entity.getCapability(ModCapabilities.UMISU_CURRENT_ENERGY_CAPABILITY).ifPresent(handler -> {
                for (ItemStack item : list) {
                    if (handler.getEnergyStored() >= energyCost) {
                        handler.extractEnergy(energyCost, false);
                        ItemEntity itemEntity = new ItemEntity(entity.getLevel(),
                                entity.getBlockPos().getX() + entity.getBlockState().getValue(direction).getOpposite().getStepX() + 0.5,
                                entity.getBlockPos().getY() + 0.2,
                                entity.getBlockPos().getZ() + entity.getBlockState().getValue(direction).getOpposite().getStepZ() + 0.5,
                                item);
                        entity.getLevel().addFreshEntity(itemEntity);
                    }
                }
            });
        }
    }

    public static int calculatePressure(Level pLevel, BlockPos pPos) {
        int count = 0;
        if (pLevel.getBlockState(pPos.above()).is(BlockRegistry.FUKAMIZU_BOOSTER.get())) {
            var be = pLevel.getBlockEntity(pPos.above());
            if (be instanceof FukamizuBoosterBlockEntity blockEntity) {
                return blockEntity.getPressure();
            }
        }

        while (pLevel.getBlockState(pPos.above()).is(Blocks.WATER)) {
            count++;
            pPos = pPos.above();
        }
        return count;
    }
}
